package qf.sail.service.impl;

import qf.sail.dao.GoodDao;
import qf.sail.dao.OrderItemDao;
import qf.sail.dao.impl.GoodDaoImpl;
import qf.sail.dao.impl.OrderItemDaoImpl;
import qf.sail.entity.Good;
import qf.sail.entity.OrderItem;
import qf.sail.service.GoodService;

import java.util.List;

public class GoodServiceImpl implements GoodService {
    private GoodDao goodDao= new GoodDaoImpl();

    private OrderItemDao orderItemDao = new OrderItemDaoImpl();

    @Override
    public List<Good> getMaxSalesGood(int count) {
        return goodDao.getMaxSalesGood(count);
    }

    @Override
    public List<Good> getMaxSalesGood(int count, int sexType) {
        return goodDao.getMaxSalesGood(count,sexType);
    }

    @Override
    public Good getGoodById(Long id) {
        return goodDao.getGoodById(id);
    }

    @Override
    public int updateGood(long orderId) {
        for(OrderItem item:orderItemDao.getItemsByOrderId(orderId)){
            Good good = goodDao.getGoodById(item.getGoodId());
            goodDao.updateGood(good.getInventory()-item.getCount(),good.getSalesVolume()+item.getCount(),good.getId());
            item.setGood(good);
        }
        return 1;
    }
}
